{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Week04"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 本周内容介绍\n",
    "\n",
    "![](https://pandas.pydata.org/pandas-docs/version/1.0.2/_images/05_newcolumn_2.svg)\n",
    "\n",
    "### 数据框新增变量 (new column/variable)\n",
    "\n",
    "如何创建从现有列派生的新列？\n",
    "\n",
    "无需遍历数据表的所有行即可进行计算。针对列(变量)的数据进行操作，一一改变转换元素的值。基于数据框其他现有数据新增变量的方法很简单：\n",
    "\n",
    "1. 直接赋值\n",
    "2. 通过assign\n",
    "\n",
    "![](https://pandas.pydata.org/pandas-docs/version/1.0.2/_images/07_melt.svg)\n",
    "\n",
    "### 数据框重塑 (reshape)\n",
    "\n",
    "如何重塑数据框的布局，以求各种数据透视？\n",
    "\n",
    "以多种方式更改数据表的结构。您可以melt()从宽到长/整齐的格式或pivot() 从长到宽格式的数据表。使用内置的聚合，可以使用sinlge命令创建数据透视表。\n",
    "\n",
    "1. 经过统计得到多维度指标数据\n",
    "2. 使用unstack实现数据二维透视  \n",
    "\n",
    "![](https://pandas.pydata.org/pandas-docs/version/1.0.2/_images/08_concat_row.svg)\n",
    "\n",
    "### 数据框聚合(merge)\n",
    "\n",
    "如何合并来自多个表的数据？\n",
    "\n",
    "可以按列将多个数据框连接为新的数据框，并且可以提供类似数据库的联接/合并操作来组合多个数据框。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 新变量处理\n",
    "## Pandas字符串处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 首先调取pandas库，info函数看int和object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 494 entries, 0 to 493\n",
      "Data columns (total 10 columns):\n",
      "排名              494 non-null int64\n",
      "企业名称            494 non-null object\n",
      "Company Name    494 non-null object\n",
      "估值（亿人民币）        494 non-null int64\n",
      "国家              494 non-null object\n",
      "城市              494 non-null object\n",
      "行业              494 non-null object\n",
      "掌门人/创始人         494 non-null object\n",
      "成立年份            494 non-null int64\n",
      "部分投资机构          494 non-null object\n",
      "dtypes: int64(3), object(7)\n",
      "memory usage: 38.7+ KB\n"
     ]
    }
   ],
   "source": [
    "# A-0 简单读档并查看数据框讯息\n",
    "\n",
    "df = pd.read_csv (\"20春_pandas_week02_hurun_unicorn.tsv\", encoding = \"utf8\", sep=\"\\t\")\n",
    "df.info()\n",
    "# 注意看Dtype! "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>排名</th>\n",
       "      <th>企业名称</th>\n",
       "      <th>Company Name</th>\n",
       "      <th>估值（亿人民币）</th>\n",
       "      <th>国家</th>\n",
       "      <th>城市</th>\n",
       "      <th>行业</th>\n",
       "      <th>掌门人/创始人</th>\n",
       "      <th>成立年份</th>\n",
       "      <th>部分投资机构</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>Ant Financial</td>\n",
       "      <td>10000</td>\n",
       "      <td>中国</td>\n",
       "      <td>杭州</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>井贤栋</td>\n",
       "      <td>2014</td>\n",
       "      <td>春华资本、中投海外、红杉资本</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>字节跳动</td>\n",
       "      <td>Bytedance</td>\n",
       "      <td>5000</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>媒体和娱乐</td>\n",
       "      <td>张一鸣</td>\n",
       "      <td>2012</td>\n",
       "      <td>红杉资本、海纳亚洲、纪源资本、启明创投</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>滴滴出行</td>\n",
       "      <td>Didi Chuxing</td>\n",
       "      <td>3600</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>共享经济</td>\n",
       "      <td>程维</td>\n",
       "      <td>2012</td>\n",
       "      <td>腾讯、阿里巴巴、红杉资本、经纬中国、纪源资本</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>Infor</td>\n",
       "      <td>Infor</td>\n",
       "      <td>3500</td>\n",
       "      <td>美国</td>\n",
       "      <td>纽约</td>\n",
       "      <td>云计算</td>\n",
       "      <td>Jim Schaper</td>\n",
       "      <td>2002</td>\n",
       "      <td>Golden Gate Capital, Koch Equity Development</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>3400</td>\n",
       "      <td>美国</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>消费品</td>\n",
       "      <td>Adam Bowen, James Monsees, Kevin Burns, Tim Da...</td>\n",
       "      <td>2015</td>\n",
       "      <td>M13, Timothy Davis, Evolution VC Partners, Tig...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>489</td>\n",
       "      <td>264</td>\n",
       "      <td>Zeta Global</td>\n",
       "      <td>Zeta Global</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>纽约</td>\n",
       "      <td>人工智能</td>\n",
       "      <td>David A. Steinberg, John Sculley</td>\n",
       "      <td>2007</td>\n",
       "      <td>GPI Capital, GSO Capital Partners</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>264</td>\n",
       "      <td>掌门1对1</td>\n",
       "      <td>Zhangmen</td>\n",
       "      <td>70</td>\n",
       "      <td>中国</td>\n",
       "      <td>上海</td>\n",
       "      <td>教育科技</td>\n",
       "      <td>张翼</td>\n",
       "      <td>2014</td>\n",
       "      <td>顺为资本、达晨创投、华平投资</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>264</td>\n",
       "      <td>转转</td>\n",
       "      <td>Zhuanzhuan</td>\n",
       "      <td>70</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>姚劲波</td>\n",
       "      <td>2015</td>\n",
       "      <td>腾讯</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>264</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>半月湾</td>\n",
       "      <td>物流</td>\n",
       "      <td>Keenan Wyrobek, Keller Rinaudo, Will Hetzler</td>\n",
       "      <td>2014</td>\n",
       "      <td>Sequoia Capital, Visionnaire Ventures, Katalys...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>264</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>洛杉矶</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>Ian Siegel, Joe Edmonds, Ward Poulos, Willis Redd</td>\n",
       "      <td>2010</td>\n",
       "      <td>IVP (Institutional Venture Partners)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>494 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      排名                   企业名称           Company Name  估值（亿人民币）  国家   城市  \\\n",
       "0      1                   蚂蚁金服          Ant Financial     10000  中国   杭州   \n",
       "1      2                   字节跳动              Bytedance      5000  中国   北京   \n",
       "2      3                   滴滴出行           Didi Chuxing      3600  中国   北京   \n",
       "3      4                  Infor                  Infor      3500  美国   纽约   \n",
       "4      5              JUUL Labs              JUUL Labs      3400  美国  旧金山   \n",
       "..   ...                    ...                    ...       ...  ..  ...   \n",
       "489  264            Zeta Global            Zeta Global        70  美国   纽约   \n",
       "490  264                  掌门1对1               Zhangmen        70  中国   上海   \n",
       "491  264                     转转             Zhuanzhuan        70  中国   北京   \n",
       "492  264  Zipline International  Zipline International        70  美国  半月湾   \n",
       "493  264           ZipRecruiter           ZipRecruiter        70  美国  洛杉矶   \n",
       "\n",
       "        行业                                            掌门人/创始人  成立年份  \\\n",
       "0     金融科技                                                井贤栋  2014   \n",
       "1    媒体和娱乐                                                张一鸣  2012   \n",
       "2     共享经济                                                 程维  2012   \n",
       "3      云计算                                        Jim Schaper  2002   \n",
       "4      消费品  Adam Bowen, James Monsees, Kevin Burns, Tim Da...  2015   \n",
       "..     ...                                                ...   ...   \n",
       "489   人工智能                   David A. Steinberg, John Sculley  2007   \n",
       "490   教育科技                                                 张翼  2014   \n",
       "491   电子商务                                                姚劲波  2015   \n",
       "492     物流       Keenan Wyrobek, Keller Rinaudo, Will Hetzler  2014   \n",
       "493   电子商务  Ian Siegel, Joe Edmonds, Ward Poulos, Willis Redd  2010   \n",
       "\n",
       "                                                部分投资机构  \n",
       "0                                       春华资本、中投海外、红杉资本  \n",
       "1                                  红杉资本、海纳亚洲、纪源资本、启明创投  \n",
       "2                               腾讯、阿里巴巴、红杉资本、经纬中国、纪源资本  \n",
       "3         Golden Gate Capital, Koch Equity Development  \n",
       "4    M13, Timothy Davis, Evolution VC Partners, Tig...  \n",
       "..                                                 ...  \n",
       "489                  GPI Capital, GSO Capital Partners  \n",
       "490                                     顺为资本、达晨创投、华平投资  \n",
       "491                                                 腾讯  \n",
       "492  Sequoia Capital, Visionnaire Ventures, Katalys...  \n",
       "493               IVP (Institutional Venture Partners)  \n",
       "\n",
       "[494 rows x 10 columns]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取某列Series的str属性，使用各种字符串处理函数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* str.split()有三个参数：第一个参数就是引号里的内容：就是分列的依据，可以是空格，符号，字符串等等。\n",
    "* 第二个参数就是前面用到的expand=True，这个参数直接将分列后的结果转换成DataFrame。拆分为表格，清楚看到每个企业有几个资本投资，没有就用none表示\n",
    "* 第三个参数的n=数字就是限制分列的次数。就是当用于分列的依据符号在有多个的话需要指定分列的次数（不指定的话就会根据符号有几个分列几次）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0                                     [春华资本, 中投海外, 红杉资本]\n",
       "1                               [红杉资本, 海纳亚洲, 纪源资本, 启明创投]\n",
       "2                           [腾讯, 阿里巴巴, 红杉资本, 经纬中国, 纪源资本]\n",
       "3         [Golden Gate Capital, Koch Equity Development]\n",
       "4      [M13, Timothy Davis, Evolution VC Partners, Ti...\n",
       "                             ...                        \n",
       "489                  [GPI Capital, GSO Capital Partners]\n",
       "490                                   [顺为资本, 达晨创投, 华平投资]\n",
       "491                                                 [腾讯]\n",
       "492    [Sequoia Capital, Visionnaire Ventures, Kataly...\n",
       "493               [IVP (Institutional Venture Partners)]\n",
       "Name: 部分投资机构, Length: 494, dtype: object"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-2 实现字符串的拆分函数split()\n",
    "df[\"部分投资机构\"].str.split('、')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>春华资本</td>\n",
       "      <td>中投海外</td>\n",
       "      <td>红杉资本</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>红杉资本</td>\n",
       "      <td>海纳亚洲</td>\n",
       "      <td>纪源资本</td>\n",
       "      <td>启明创投</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>腾讯</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>红杉资本</td>\n",
       "      <td>经纬中国</td>\n",
       "      <td>纪源资本</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Golden Gate Capital, Koch Equity Development</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>M13, Timothy Davis, Evolution VC Partners, Tig...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>489</td>\n",
       "      <td>GPI Capital, GSO Capital Partners</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>顺为资本</td>\n",
       "      <td>达晨创投</td>\n",
       "      <td>华平投资</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>腾讯</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>Sequoia Capital, Visionnaire Ventures, Katalys...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>IVP (Institutional Venture Partners)</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>494 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     0     1     2     3  \\\n",
       "0                                                 春华资本  中投海外  红杉资本  None   \n",
       "1                                                 红杉资本  海纳亚洲  纪源资本  启明创投   \n",
       "2                                                   腾讯  阿里巴巴  红杉资本  经纬中国   \n",
       "3         Golden Gate Capital, Koch Equity Development  None  None  None   \n",
       "4    M13, Timothy Davis, Evolution VC Partners, Tig...  None  None  None   \n",
       "..                                                 ...   ...   ...   ...   \n",
       "489                  GPI Capital, GSO Capital Partners  None  None  None   \n",
       "490                                               顺为资本  达晨创投  华平投资  None   \n",
       "491                                                 腾讯  None  None  None   \n",
       "492  Sequoia Capital, Visionnaire Ventures, Katalys...  None  None  None   \n",
       "493               IVP (Institutional Venture Partners)  None  None  None   \n",
       "\n",
       "        4     5  \n",
       "0    None  None  \n",
       "1    None  None  \n",
       "2    纪源资本  None  \n",
       "3    None  None  \n",
       "4    None  None  \n",
       "..    ...   ...  \n",
       "489  None  None  \n",
       "490  None  None  \n",
       "491  None  None  \n",
       "492  None  None  \n",
       "493  None  None  \n",
       "\n",
       "[494 rows x 6 columns]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-2 Extra expand\n",
    "df[\"部分投资机构\"].str.split('、',expand=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>春华资本</td>\n",
       "      <td>中投海外</td>\n",
       "      <td>红杉资本</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>红杉资本</td>\n",
       "      <td>海纳亚洲</td>\n",
       "      <td>纪源资本</td>\n",
       "      <td>启明创投</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>腾讯</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>红杉资本</td>\n",
       "      <td>经纬中国</td>\n",
       "      <td>纪源资本</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Golden Gate Capital, Koch Equity Development</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>M13, Timothy Davis, Evolution VC Partners, Tig...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>489</td>\n",
       "      <td>GPI Capital, GSO Capital Partners</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>顺为资本</td>\n",
       "      <td>达晨创投</td>\n",
       "      <td>华平投资</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>腾讯</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>Sequoia Capital, Visionnaire Ventures, Katalys...</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>IVP (Institutional Venture Partners)</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>494 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     0     1     2     3     4\n",
       "0                                                 春华资本  中投海外  红杉资本  None  None\n",
       "1                                                 红杉资本  海纳亚洲  纪源资本  启明创投  None\n",
       "2                                                   腾讯  阿里巴巴  红杉资本  经纬中国  纪源资本\n",
       "3         Golden Gate Capital, Koch Equity Development  None  None  None  None\n",
       "4    M13, Timothy Davis, Evolution VC Partners, Tig...  None  None  None  None\n",
       "..                                                 ...   ...   ...   ...   ...\n",
       "489                  GPI Capital, GSO Capital Partners  None  None  None  None\n",
       "490                                               顺为资本  达晨创投  华平投资  None  None\n",
       "491                                                 腾讯  None  None  None  None\n",
       "492  Sequoia Capital, Visionnaire Ventures, Katalys...  None  None  None  None\n",
       "493               IVP (Institutional Venture Partners)  None  None  None  None\n",
       "\n",
       "[494 rows x 5 columns]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"部分投资机构\"].str.split('、',expand=True,n=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       True\n",
       "1       True\n",
       "2       True\n",
       "3      False\n",
       "4      False\n",
       "       ...  \n",
       "489    False\n",
       "490    False\n",
       "491    False\n",
       "492    False\n",
       "493    False\n",
       "Name: 部分投资机构, Length: 494, dtype: bool"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-3 可用contains方法查询是否包含某些字符\n",
    "df[\"部分投资机构\"].str.contains(\"红杉资本\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 补充 | 可同时查询多个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       True\n",
       "1       True\n",
       "2       True\n",
       "3      False\n",
       "4      False\n",
       "       ...  \n",
       "489    False\n",
       "490    False\n",
       "491     True\n",
       "492    False\n",
       "493    False\n",
       "Name: 部分投资机构, Length: 494, dtype: bool"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"部分投资机构\"].str.contains(\"红杉资本|腾讯\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      False\n",
       "1      False\n",
       "2      False\n",
       "3      False\n",
       "4      False\n",
       "       ...  \n",
       "489    False\n",
       "490    False\n",
       "491    False\n",
       "492    False\n",
       "493    False\n",
       "Name: 企业名称, Length: 494, dtype: bool"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-4 额外讲解（1）：isnumeric 判断是否为数字\n",
    "df[\"企业名称\"].str.isnumeric()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "Can only use .str accessor with string values!",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-64-a4617a300694>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# A-5 额外讲解（2）数字列是无法使用str方法的\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"部分投资机构\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"估值（亿人民币）\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m   5173\u001b[0m             \u001b[1;32mor\u001b[0m \u001b[0mname\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_accessors\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5174\u001b[0m         ):\n\u001b[1;32m-> 5175\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mobject\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__getattribute__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   5176\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   5177\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_info_axis\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_can_hold_identifiers_and_holds_name\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\accessor.py\u001b[0m in \u001b[0;36m__get__\u001b[1;34m(self, obj, cls)\u001b[0m\n\u001b[0;32m    173\u001b[0m             \u001b[1;31m# we're accessing the attribute of the class, i.e., Dataset.geo\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    174\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_accessor\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 175\u001b[1;33m         \u001b[0maccessor_obj\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_accessor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    176\u001b[0m         \u001b[1;31m# Replace the property with the accessor object. Inspired by:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    177\u001b[0m         \u001b[1;31m# http://www.pydanny.com/cached-property.html\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\strings.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, data)\u001b[0m\n\u001b[0;32m   1915\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1916\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1917\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_inferred_dtype\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_validate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1918\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_is_categorical\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mis_categorical_dtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1919\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\strings.py\u001b[0m in \u001b[0;36m_validate\u001b[1;34m(data)\u001b[0m\n\u001b[0;32m   1965\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1966\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0minferred_dtype\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mallowed_types\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1967\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mAttributeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Can only use .str accessor with string \"\u001b[0m \u001b[1;34m\"values!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1968\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0minferred_dtype\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1969\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: Can only use .str accessor with string values!"
     ]
    }
   ],
   "source": [
    "# A-5 额外讲解（2）数字列是无法使用str方法的\n",
    "df[\"部分投资机构\"].str.len()\n",
    "df[\"估值（亿人民币）\"].str.len()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 新增一列\n",
    "* 1. 直接赋值 把拆分好的list直接加到后面，用到切片在后面直接赋值，先通过字符串操作保留放到后面\n",
    "* 2. df.assign方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "# B-1 直接赋值\n",
    "df.loc[:,\"部分投资机构_list\"]=df[\"部分投资机构\"].str.split('、')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>排名</th>\n",
       "      <th>企业名称</th>\n",
       "      <th>Company Name</th>\n",
       "      <th>估值（亿人民币）</th>\n",
       "      <th>国家</th>\n",
       "      <th>城市</th>\n",
       "      <th>行业</th>\n",
       "      <th>掌门人/创始人</th>\n",
       "      <th>成立年份</th>\n",
       "      <th>部分投资机构</th>\n",
       "      <th>部分投资机构_list</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>Ant Financial</td>\n",
       "      <td>10000</td>\n",
       "      <td>中国</td>\n",
       "      <td>杭州</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>井贤栋</td>\n",
       "      <td>2014</td>\n",
       "      <td>春华资本、中投海外、红杉资本</td>\n",
       "      <td>[春华资本, 中投海外, 红杉资本]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>字节跳动</td>\n",
       "      <td>Bytedance</td>\n",
       "      <td>5000</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>媒体和娱乐</td>\n",
       "      <td>张一鸣</td>\n",
       "      <td>2012</td>\n",
       "      <td>红杉资本、海纳亚洲、纪源资本、启明创投</td>\n",
       "      <td>[红杉资本, 海纳亚洲, 纪源资本, 启明创投]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>滴滴出行</td>\n",
       "      <td>Didi Chuxing</td>\n",
       "      <td>3600</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>共享经济</td>\n",
       "      <td>程维</td>\n",
       "      <td>2012</td>\n",
       "      <td>腾讯、阿里巴巴、红杉资本、经纬中国、纪源资本</td>\n",
       "      <td>[腾讯, 阿里巴巴, 红杉资本, 经纬中国, 纪源资本]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>Infor</td>\n",
       "      <td>Infor</td>\n",
       "      <td>3500</td>\n",
       "      <td>美国</td>\n",
       "      <td>纽约</td>\n",
       "      <td>云计算</td>\n",
       "      <td>Jim Schaper</td>\n",
       "      <td>2002</td>\n",
       "      <td>Golden Gate Capital, Koch Equity Development</td>\n",
       "      <td>[Golden Gate Capital, Koch Equity Development]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>3400</td>\n",
       "      <td>美国</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>消费品</td>\n",
       "      <td>Adam Bowen, James Monsees, Kevin Burns, Tim Da...</td>\n",
       "      <td>2015</td>\n",
       "      <td>M13, Timothy Davis, Evolution VC Partners, Tig...</td>\n",
       "      <td>[M13, Timothy Davis, Evolution VC Partners, Ti...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>489</td>\n",
       "      <td>264</td>\n",
       "      <td>Zeta Global</td>\n",
       "      <td>Zeta Global</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>纽约</td>\n",
       "      <td>人工智能</td>\n",
       "      <td>David A. Steinberg, John Sculley</td>\n",
       "      <td>2007</td>\n",
       "      <td>GPI Capital, GSO Capital Partners</td>\n",
       "      <td>[GPI Capital, GSO Capital Partners]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>264</td>\n",
       "      <td>掌门1对1</td>\n",
       "      <td>Zhangmen</td>\n",
       "      <td>70</td>\n",
       "      <td>中国</td>\n",
       "      <td>上海</td>\n",
       "      <td>教育科技</td>\n",
       "      <td>张翼</td>\n",
       "      <td>2014</td>\n",
       "      <td>顺为资本、达晨创投、华平投资</td>\n",
       "      <td>[顺为资本, 达晨创投, 华平投资]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>264</td>\n",
       "      <td>转转</td>\n",
       "      <td>Zhuanzhuan</td>\n",
       "      <td>70</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>姚劲波</td>\n",
       "      <td>2015</td>\n",
       "      <td>腾讯</td>\n",
       "      <td>[腾讯]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>264</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>半月湾</td>\n",
       "      <td>物流</td>\n",
       "      <td>Keenan Wyrobek, Keller Rinaudo, Will Hetzler</td>\n",
       "      <td>2014</td>\n",
       "      <td>Sequoia Capital, Visionnaire Ventures, Katalys...</td>\n",
       "      <td>[Sequoia Capital, Visionnaire Ventures, Kataly...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>264</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>洛杉矶</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>Ian Siegel, Joe Edmonds, Ward Poulos, Willis Redd</td>\n",
       "      <td>2010</td>\n",
       "      <td>IVP (Institutional Venture Partners)</td>\n",
       "      <td>[IVP (Institutional Venture Partners)]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>494 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      排名                   企业名称           Company Name  估值（亿人民币）  国家   城市  \\\n",
       "0      1                   蚂蚁金服          Ant Financial     10000  中国   杭州   \n",
       "1      2                   字节跳动              Bytedance      5000  中国   北京   \n",
       "2      3                   滴滴出行           Didi Chuxing      3600  中国   北京   \n",
       "3      4                  Infor                  Infor      3500  美国   纽约   \n",
       "4      5              JUUL Labs              JUUL Labs      3400  美国  旧金山   \n",
       "..   ...                    ...                    ...       ...  ..  ...   \n",
       "489  264            Zeta Global            Zeta Global        70  美国   纽约   \n",
       "490  264                  掌门1对1               Zhangmen        70  中国   上海   \n",
       "491  264                     转转             Zhuanzhuan        70  中国   北京   \n",
       "492  264  Zipline International  Zipline International        70  美国  半月湾   \n",
       "493  264           ZipRecruiter           ZipRecruiter        70  美国  洛杉矶   \n",
       "\n",
       "        行业                                            掌门人/创始人  成立年份  \\\n",
       "0     金融科技                                                井贤栋  2014   \n",
       "1    媒体和娱乐                                                张一鸣  2012   \n",
       "2     共享经济                                                 程维  2012   \n",
       "3      云计算                                        Jim Schaper  2002   \n",
       "4      消费品  Adam Bowen, James Monsees, Kevin Burns, Tim Da...  2015   \n",
       "..     ...                                                ...   ...   \n",
       "489   人工智能                   David A. Steinberg, John Sculley  2007   \n",
       "490   教育科技                                                 张翼  2014   \n",
       "491   电子商务                                                姚劲波  2015   \n",
       "492     物流       Keenan Wyrobek, Keller Rinaudo, Will Hetzler  2014   \n",
       "493   电子商务  Ian Siegel, Joe Edmonds, Ward Poulos, Willis Redd  2010   \n",
       "\n",
       "                                                部分投资机构  \\\n",
       "0                                       春华资本、中投海外、红杉资本   \n",
       "1                                  红杉资本、海纳亚洲、纪源资本、启明创投   \n",
       "2                               腾讯、阿里巴巴、红杉资本、经纬中国、纪源资本   \n",
       "3         Golden Gate Capital, Koch Equity Development   \n",
       "4    M13, Timothy Davis, Evolution VC Partners, Tig...   \n",
       "..                                                 ...   \n",
       "489                  GPI Capital, GSO Capital Partners   \n",
       "490                                     顺为资本、达晨创投、华平投资   \n",
       "491                                                 腾讯   \n",
       "492  Sequoia Capital, Visionnaire Ventures, Katalys...   \n",
       "493               IVP (Institutional Venture Partners)   \n",
       "\n",
       "                                           部分投资机构_list  \n",
       "0                                   [春华资本, 中投海外, 红杉资本]  \n",
       "1                             [红杉资本, 海纳亚洲, 纪源资本, 启明创投]  \n",
       "2                         [腾讯, 阿里巴巴, 红杉资本, 经纬中国, 纪源资本]  \n",
       "3       [Golden Gate Capital, Koch Equity Development]  \n",
       "4    [M13, Timothy Davis, Evolution VC Partners, Ti...  \n",
       "..                                                 ...  \n",
       "489                [GPI Capital, GSO Capital Partners]  \n",
       "490                                 [顺为资本, 达晨创投, 华平投资]  \n",
       "491                                               [腾讯]  \n",
       "492  [Sequoia Capital, Visionnaire Ventures, Kataly...  \n",
       "493             [IVP (Institutional Venture Partners)]  \n",
       "\n",
       "[494 rows x 11 columns]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "# B-2 df.assign方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>排名</th>\n",
       "      <th>企业名称</th>\n",
       "      <th>Company Name</th>\n",
       "      <th>估值（亿人民币）</th>\n",
       "      <th>国家</th>\n",
       "      <th>城市</th>\n",
       "      <th>行业</th>\n",
       "      <th>掌门人/创始人</th>\n",
       "      <th>成立年份</th>\n",
       "      <th>部分投资机构</th>\n",
       "      <th>部分投资机构_list</th>\n",
       "      <th>掌门人创始人_list</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>Ant Financial</td>\n",
       "      <td>10000</td>\n",
       "      <td>中国</td>\n",
       "      <td>杭州</td>\n",
       "      <td>金融科技</td>\n",
       "      <td>井贤栋</td>\n",
       "      <td>2014</td>\n",
       "      <td>春华资本、中投海外、红杉资本</td>\n",
       "      <td>[春华资本, 中投海外, 红杉资本]</td>\n",
       "      <td>[井贤栋]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>字节跳动</td>\n",
       "      <td>Bytedance</td>\n",
       "      <td>5000</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>媒体和娱乐</td>\n",
       "      <td>张一鸣</td>\n",
       "      <td>2012</td>\n",
       "      <td>红杉资本、海纳亚洲、纪源资本、启明创投</td>\n",
       "      <td>[红杉资本, 海纳亚洲, 纪源资本, 启明创投]</td>\n",
       "      <td>[张一鸣]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>滴滴出行</td>\n",
       "      <td>Didi Chuxing</td>\n",
       "      <td>3600</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>共享经济</td>\n",
       "      <td>程维</td>\n",
       "      <td>2012</td>\n",
       "      <td>腾讯、阿里巴巴、红杉资本、经纬中国、纪源资本</td>\n",
       "      <td>[腾讯, 阿里巴巴, 红杉资本, 经纬中国, 纪源资本]</td>\n",
       "      <td>[程维]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>Infor</td>\n",
       "      <td>Infor</td>\n",
       "      <td>3500</td>\n",
       "      <td>美国</td>\n",
       "      <td>纽约</td>\n",
       "      <td>云计算</td>\n",
       "      <td>Jim Schaper</td>\n",
       "      <td>2002</td>\n",
       "      <td>Golden Gate Capital, Koch Equity Development</td>\n",
       "      <td>[Golden Gate Capital,  Koch Equity Development]</td>\n",
       "      <td>[Jim Schaper]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>3400</td>\n",
       "      <td>美国</td>\n",
       "      <td>旧金山</td>\n",
       "      <td>消费品</td>\n",
       "      <td>Adam Bowen, James Monsees, Kevin Burns, Tim Da...</td>\n",
       "      <td>2015</td>\n",
       "      <td>M13, Timothy Davis, Evolution VC Partners, Tig...</td>\n",
       "      <td>[M13,  Timothy Davis,  Evolution VC Partners, ...</td>\n",
       "      <td>[Adam Bowen,  James Monsees,  Kevin Burns,  Ti...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>489</td>\n",
       "      <td>264</td>\n",
       "      <td>Zeta Global</td>\n",
       "      <td>Zeta Global</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>纽约</td>\n",
       "      <td>人工智能</td>\n",
       "      <td>David A. Steinberg, John Sculley</td>\n",
       "      <td>2007</td>\n",
       "      <td>GPI Capital, GSO Capital Partners</td>\n",
       "      <td>[GPI Capital,  GSO Capital Partners]</td>\n",
       "      <td>[David A. Steinberg,  John Sculley]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>264</td>\n",
       "      <td>掌门1对1</td>\n",
       "      <td>Zhangmen</td>\n",
       "      <td>70</td>\n",
       "      <td>中国</td>\n",
       "      <td>上海</td>\n",
       "      <td>教育科技</td>\n",
       "      <td>张翼</td>\n",
       "      <td>2014</td>\n",
       "      <td>顺为资本、达晨创投、华平投资</td>\n",
       "      <td>[顺为资本, 达晨创投, 华平投资]</td>\n",
       "      <td>[张翼]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>264</td>\n",
       "      <td>转转</td>\n",
       "      <td>Zhuanzhuan</td>\n",
       "      <td>70</td>\n",
       "      <td>中国</td>\n",
       "      <td>北京</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>姚劲波</td>\n",
       "      <td>2015</td>\n",
       "      <td>腾讯</td>\n",
       "      <td>[腾讯]</td>\n",
       "      <td>[姚劲波]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>264</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>半月湾</td>\n",
       "      <td>物流</td>\n",
       "      <td>Keenan Wyrobek, Keller Rinaudo, Will Hetzler</td>\n",
       "      <td>2014</td>\n",
       "      <td>Sequoia Capital, Visionnaire Ventures, Katalys...</td>\n",
       "      <td>[Sequoia Capital,  Visionnaire Ventures,  Kata...</td>\n",
       "      <td>[Keenan Wyrobek,  Keller Rinaudo,  Will Hetzler]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>264</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>70</td>\n",
       "      <td>美国</td>\n",
       "      <td>洛杉矶</td>\n",
       "      <td>电子商务</td>\n",
       "      <td>Ian Siegel, Joe Edmonds, Ward Poulos, Willis Redd</td>\n",
       "      <td>2010</td>\n",
       "      <td>IVP (Institutional Venture Partners)</td>\n",
       "      <td>[IVP (Institutional Venture Partners)]</td>\n",
       "      <td>[Ian Siegel,  Joe Edmonds,  Ward Poulos,  Will...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>494 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      排名                   企业名称           Company Name  估值（亿人民币）  国家   城市  \\\n",
       "0      1                   蚂蚁金服          Ant Financial     10000  中国   杭州   \n",
       "1      2                   字节跳动              Bytedance      5000  中国   北京   \n",
       "2      3                   滴滴出行           Didi Chuxing      3600  中国   北京   \n",
       "3      4                  Infor                  Infor      3500  美国   纽约   \n",
       "4      5              JUUL Labs              JUUL Labs      3400  美国  旧金山   \n",
       "..   ...                    ...                    ...       ...  ..  ...   \n",
       "489  264            Zeta Global            Zeta Global        70  美国   纽约   \n",
       "490  264                  掌门1对1               Zhangmen        70  中国   上海   \n",
       "491  264                     转转             Zhuanzhuan        70  中国   北京   \n",
       "492  264  Zipline International  Zipline International        70  美国  半月湾   \n",
       "493  264           ZipRecruiter           ZipRecruiter        70  美国  洛杉矶   \n",
       "\n",
       "        行业                                            掌门人/创始人  成立年份  \\\n",
       "0     金融科技                                                井贤栋  2014   \n",
       "1    媒体和娱乐                                                张一鸣  2012   \n",
       "2     共享经济                                                 程维  2012   \n",
       "3      云计算                                        Jim Schaper  2002   \n",
       "4      消费品  Adam Bowen, James Monsees, Kevin Burns, Tim Da...  2015   \n",
       "..     ...                                                ...   ...   \n",
       "489   人工智能                   David A. Steinberg, John Sculley  2007   \n",
       "490   教育科技                                                 张翼  2014   \n",
       "491   电子商务                                                姚劲波  2015   \n",
       "492     物流       Keenan Wyrobek, Keller Rinaudo, Will Hetzler  2014   \n",
       "493   电子商务  Ian Siegel, Joe Edmonds, Ward Poulos, Willis Redd  2010   \n",
       "\n",
       "                                                部分投资机构  \\\n",
       "0                                       春华资本、中投海外、红杉资本   \n",
       "1                                  红杉资本、海纳亚洲、纪源资本、启明创投   \n",
       "2                               腾讯、阿里巴巴、红杉资本、经纬中国、纪源资本   \n",
       "3         Golden Gate Capital, Koch Equity Development   \n",
       "4    M13, Timothy Davis, Evolution VC Partners, Tig...   \n",
       "..                                                 ...   \n",
       "489                  GPI Capital, GSO Capital Partners   \n",
       "490                                     顺为资本、达晨创投、华平投资   \n",
       "491                                                 腾讯   \n",
       "492  Sequoia Capital, Visionnaire Ventures, Katalys...   \n",
       "493               IVP (Institutional Venture Partners)   \n",
       "\n",
       "                                           部分投资机构_list  \\\n",
       "0                                   [春华资本, 中投海外, 红杉资本]   \n",
       "1                             [红杉资本, 海纳亚洲, 纪源资本, 启明创投]   \n",
       "2                         [腾讯, 阿里巴巴, 红杉资本, 经纬中国, 纪源资本]   \n",
       "3      [Golden Gate Capital,  Koch Equity Development]   \n",
       "4    [M13,  Timothy Davis,  Evolution VC Partners, ...   \n",
       "..                                                 ...   \n",
       "489               [GPI Capital,  GSO Capital Partners]   \n",
       "490                                 [顺为资本, 达晨创投, 华平投资]   \n",
       "491                                               [腾讯]   \n",
       "492  [Sequoia Capital,  Visionnaire Ventures,  Kata...   \n",
       "493             [IVP (Institutional Venture Partners)]   \n",
       "\n",
       "                                           掌门人创始人_list  \n",
       "0                                                [井贤栋]  \n",
       "1                                                [张一鸣]  \n",
       "2                                                 [程维]  \n",
       "3                                        [Jim Schaper]  \n",
       "4    [Adam Bowen,  James Monsees,  Kevin Burns,  Ti...  \n",
       "..                                                 ...  \n",
       "489                [David A. Steinberg,  John Sculley]  \n",
       "490                                               [张翼]  \n",
       "491                                              [姚劲波]  \n",
       "492   [Keenan Wyrobek,  Keller Rinaudo,  Will Hetzler]  \n",
       "493  [Ian Siegel,  Joe Edmonds,  Ward Poulos,  Will...  \n",
       "\n",
       "[494 rows x 12 columns]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以同时添加多个新的列\n",
    "df.assign(\n",
    "    部分投资机构_list = lambda x : x[\"部分投资机构\"].str.split('[、,,]'),\n",
    "    掌门人创始人_list = lambda x : x[\"掌门人/创始人\"].str.split('[、,,]')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* assign函数同时增加多个列\n",
    "* lambda函数（匿名函数），省略函数命名直接使用X就是df列表，省略我们定义函数的部分直接用lambda+参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据框重塑\n",
    "##  Pandas的stack实现数据透视\n",
    "\n",
    "stack()是将原来的列索引转成了最内层的行索引，这里是多层次索引，其中AB索引对应第三层，即最内层索引。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](http://localhost:8888/files/111.png)\n",
    "表格在行列方向上均有索引（类似于DataFrame），花括号结构只有“列方向”上的索引（类似于层次化的Series），结构更加偏向于堆叠（Series-stack，方便记忆）。stack函数会将数据从”表格结构“变成”花括号结构“，即将其行索引变成列索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "      <th>掌门人/创始人</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>序号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>井贤栋</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>字节跳动</td>\n",
       "      <td>张一鸣</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>滴滴出行</td>\n",
       "      <td>程维</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Infor</td>\n",
       "      <td>Jim Schaper</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>Adam Bowen</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>Will Hetzler</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Ian Siegel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Joe Edmonds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Ward Poulos</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Willis Redd</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>911 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      企业名称        掌门人/创始人\n",
       "序号                                       \n",
       "0                     蚂蚁金服            井贤栋\n",
       "1                     字节跳动            张一鸣\n",
       "2                     滴滴出行             程维\n",
       "3                    Infor    Jim Schaper\n",
       "4                JUUL Labs     Adam Bowen\n",
       "..                     ...            ...\n",
       "492  Zipline International   Will Hetzler\n",
       "493           ZipRecruiter     Ian Siegel\n",
       "493           ZipRecruiter    Joe Edmonds\n",
       "493           ZipRecruiter    Ward Poulos\n",
       "493           ZipRecruiter    Willis Redd\n",
       "\n",
       "[911 rows x 2 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "      <th>部分投资机构</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>序号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>春华资本</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>中投海外</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>红杉资本</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>字节跳动</td>\n",
       "      <td>红杉资本</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>字节跳动</td>\n",
       "      <td>海纳亚洲</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>掌门1对1</td>\n",
       "      <td>达晨创投</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>掌门1对1</td>\n",
       "      <td>华平投资</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>转转</td>\n",
       "      <td>腾讯</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>Sequoia Capital, Visionnaire Ventures, Katalys...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>IVP (Institutional Venture Partners)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>885 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      企业名称                                             部分投资机构\n",
       "序号                                                                           \n",
       "0                     蚂蚁金服                                               春华资本\n",
       "0                     蚂蚁金服                                               中投海外\n",
       "0                     蚂蚁金服                                               红杉资本\n",
       "1                     字节跳动                                               红杉资本\n",
       "1                     字节跳动                                               海纳亚洲\n",
       "..                     ...                                                ...\n",
       "490                  掌门1对1                                               达晨创投\n",
       "490                  掌门1对1                                               华平投资\n",
       "491                     转转                                                 腾讯\n",
       "492  Zipline International  Sequoia Capital, Visionnaire Ventures, Katalys...\n",
       "493           ZipRecruiter               IVP (Institutional Venture Partners)\n",
       "\n",
       "[885 rows x 2 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "df_独角兽_创始人_准备 =df[[\"企业名称\",\"掌门人/创始人\"]]\n",
    "df_独角兽_创始人_准备.index.name=\"序号\"\n",
    "df_独角兽_创始人 =pd.merge(df_独角兽_创始人_准备.drop('掌门人/创始人', axis=1),\\\n",
    "                          df['掌门人/创始人'].str.split(',', expand=True)\\\n",
    "                                            .stack().reset_index(level=1,drop=True)\n",
    "                                            .rename('掌门人/创始人'),\\\n",
    "                          on=\"序号\")\n",
    "\n",
    "df_独角兽_投资机构_准备 = df[[\"企业名称\",\"部分投资机构\"]]\n",
    "df_独角兽_投资机构_准备.index.name=\"序号\"\n",
    "df_独角兽_投资机构 = pd.merge(df_独角兽_投资机构_准备.drop('部分投资机构', axis=1),\\\n",
    "                             df['部分投资机构'].str.split('、', expand=True)\\\n",
    "                                             .stack().reset_index(level=1, drop=True)\n",
    "                                             .rename('部分投资机构'),\\\n",
    "                             on=\"序号\")\n",
    "\n",
    "\n",
    "display(df_独角兽_创始人)\n",
    "display(df_独角兽_投资机构)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "      <th>掌门人/创始人</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>序号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>井贤栋</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>字节跳动</td>\n",
       "      <td>张一鸣</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>滴滴出行</td>\n",
       "      <td>程维</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Infor</td>\n",
       "      <td>Jim Schaper</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>Adam Bowen, James Monsees, Kevin Burns, Tim Da...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>489</td>\n",
       "      <td>Zeta Global</td>\n",
       "      <td>David A. Steinberg, John Sculley</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>掌门1对1</td>\n",
       "      <td>张翼</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>转转</td>\n",
       "      <td>姚劲波</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>Keenan Wyrobek, Keller Rinaudo, Will Hetzler</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Ian Siegel, Joe Edmonds, Ward Poulos, Willis Redd</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>494 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      企业名称                                            掌门人/创始人\n",
       "序号                                                                           \n",
       "0                     蚂蚁金服                                                井贤栋\n",
       "1                     字节跳动                                                张一鸣\n",
       "2                     滴滴出行                                                 程维\n",
       "3                    Infor                                        Jim Schaper\n",
       "4                JUUL Labs  Adam Bowen, James Monsees, Kevin Burns, Tim Da...\n",
       "..                     ...                                                ...\n",
       "489            Zeta Global                   David A. Steinberg, John Sculley\n",
       "490                  掌门1对1                                                 张翼\n",
       "491                     转转                                                姚劲波\n",
       "492  Zipline International       Keenan Wyrobek, Keller Rinaudo, Will Hetzler\n",
       "493           ZipRecruiter  Ian Siegel, Joe Edmonds, Ward Poulos, Willis Redd\n",
       "\n",
       "[494 rows x 2 columns]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_独角兽_创始人_准备 =df[[\"企业名称\",\"掌门人/创始人\"]]\n",
    "df_独角兽_创始人_准备"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### drop为删除某一列\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>序号</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>字节跳动</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>滴滴出行</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Infor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>JUUL Labs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>489</td>\n",
       "      <td>Zeta Global</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>掌门1对1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>转转</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>Zipline International</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>494 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      企业名称\n",
       "序号                        \n",
       "0                     蚂蚁金服\n",
       "1                     字节跳动\n",
       "2                     滴滴出行\n",
       "3                    Infor\n",
       "4                JUUL Labs\n",
       "..                     ...\n",
       "489            Zeta Global\n",
       "490                  掌门1对1\n",
       "491                     转转\n",
       "492  Zipline International\n",
       "493           ZipRecruiter\n",
       "\n",
       "[494 rows x 1 columns]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# B-2 drop()删除某列，作为准备合并的表格\n",
    "df_待合并 =df_独角兽_创始人_准备.drop('掌门人/创始人', axis=1)\n",
    "df_待合并"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### split()中参数expand可以做什么？\n",
    "#### [A]：expand表示是否把series类型转化为DataFrame类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>10</th>\n",
       "      <th>11</th>\n",
       "      <th>12</th>\n",
       "      <th>13</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>序号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>井贤栋</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>张一鸣</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>程维</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Jim Schaper</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>Adam Bowen</td>\n",
       "      <td>James Monsees</td>\n",
       "      <td>Kevin Burns</td>\n",
       "      <td>Tim Danaher</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>489</td>\n",
       "      <td>David A. Steinberg</td>\n",
       "      <td>John Sculley</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>490</td>\n",
       "      <td>张翼</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>491</td>\n",
       "      <td>姚劲波</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>Keenan Wyrobek</td>\n",
       "      <td>Keller Rinaudo</td>\n",
       "      <td>Will Hetzler</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>Ian Siegel</td>\n",
       "      <td>Joe Edmonds</td>\n",
       "      <td>Ward Poulos</td>\n",
       "      <td>Willis Redd</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>494 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      0                1              2             3     4  \\\n",
       "序号                                                                            \n",
       "0                   井贤栋             None           None          None  None   \n",
       "1                   张一鸣             None           None          None  None   \n",
       "2                    程维             None           None          None  None   \n",
       "3           Jim Schaper             None           None          None  None   \n",
       "4            Adam Bowen    James Monsees    Kevin Burns   Tim Danaher  None   \n",
       "..                  ...              ...            ...           ...   ...   \n",
       "489  David A. Steinberg     John Sculley           None          None  None   \n",
       "490                  张翼             None           None          None  None   \n",
       "491                 姚劲波             None           None          None  None   \n",
       "492      Keenan Wyrobek   Keller Rinaudo   Will Hetzler          None  None   \n",
       "493          Ian Siegel      Joe Edmonds    Ward Poulos   Willis Redd  None   \n",
       "\n",
       "        5     6     7     8     9    10    11    12    13  \n",
       "序号                                                         \n",
       "0    None  None  None  None  None  None  None  None  None  \n",
       "1    None  None  None  None  None  None  None  None  None  \n",
       "2    None  None  None  None  None  None  None  None  None  \n",
       "3    None  None  None  None  None  None  None  None  None  \n",
       "4    None  None  None  None  None  None  None  None  None  \n",
       "..    ...   ...   ...   ...   ...   ...   ...   ...   ...  \n",
       "489  None  None  None  None  None  None  None  None  None  \n",
       "490  None  None  None  None  None  None  None  None  None  \n",
       "491  None  None  None  None  None  None  None  None  None  \n",
       "492  None  None  None  None  None  None  None  None  None  \n",
       "493  None  None  None  None  None  None  None  None  None  \n",
       "\n",
       "[494 rows x 14 columns]"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# B-3 split()拆分列元素\n",
    "df['掌门人/创始人'].str.split(',', expand=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### stack对行进行索引操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "序号\n",
       "0                井贤栋\n",
       "1                张一鸣\n",
       "2                 程维\n",
       "3        Jim Schaper\n",
       "4         Adam Bowen\n",
       "           ...      \n",
       "492     Will Hetzler\n",
       "493       Ian Siegel\n",
       "493      Joe Edmonds\n",
       "493      Ward Poulos\n",
       "493      Willis Redd\n",
       "Length: 911, dtype: object"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# B-4 stack()以最内层为索引堆叠,并删除某列的索引\n",
    "df['掌门人/创始人'].str.split(',', expand=True).stack().reset_index(level=1,drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "序号\n",
       "0                井贤栋\n",
       "1                张一鸣\n",
       "2                 程维\n",
       "3        Jim Schaper\n",
       "4         Adam Bowen\n",
       "           ...      \n",
       "492     Will Hetzler\n",
       "493       Ian Siegel\n",
       "493      Joe Edmonds\n",
       "493      Ward Poulos\n",
       "493      Willis Redd\n",
       "Name: 掌门人/创始人, Length: 911, dtype: object"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# B-4 Extra 我们给新的Series命名\n",
    "df_准备合并的列 =df['掌门人/创始人'].str.split(',', expand=True).stack().reset_index(level=1,drop=True).rename('掌门人/创始人')\n",
    "df_准备合并的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>序号</th>\n",
       "      <th>掌门人/创始人</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>井贤栋</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>张一鸣</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>程维</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>Jim Schaper</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>Adam Bowen</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>James Monsees</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>Kevin Burns</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>Tim Danaher</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>Brian Chesky</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "      <td>Joe Gebbia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "      <td>Nathan Blecharczyk</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>6</td>\n",
       "      <td>计葵生</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>12</td>\n",
       "      <td>7</td>\n",
       "      <td>Elon Musk</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>13</td>\n",
       "      <td>8</td>\n",
       "      <td>Adam Neumann</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14</td>\n",
       "      <td>8</td>\n",
       "      <td>Miguel McKevley</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15</td>\n",
       "      <td>9</td>\n",
       "      <td>John Collison</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>16</td>\n",
       "      <td>9</td>\n",
       "      <td>Patrick Collison</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>17</td>\n",
       "      <td>10</td>\n",
       "      <td>顾敏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>18</td>\n",
       "      <td>11</td>\n",
       "      <td>童文红</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>19</td>\n",
       "      <td>12</td>\n",
       "      <td>陈生强</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    序号              掌门人/创始人\n",
       "0    0                  井贤栋\n",
       "1    1                  张一鸣\n",
       "2    2                   程维\n",
       "3    3          Jim Schaper\n",
       "4    4           Adam Bowen\n",
       "5    4        James Monsees\n",
       "6    4          Kevin Burns\n",
       "7    4          Tim Danaher\n",
       "8    5         Brian Chesky\n",
       "9    5           Joe Gebbia\n",
       "10   5   Nathan Blecharczyk\n",
       "11   6                  计葵生\n",
       "12   7            Elon Musk\n",
       "13   8         Adam Neumann\n",
       "14   8      Miguel McKevley\n",
       "15   9        John Collison\n",
       "16   9     Patrick Collison\n",
       "17  10                   顾敏\n",
       "18  11                  童文红\n",
       "19  12                  陈生强"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict_准备合并的列 = {\"序号\":df_准备合并的列.index,\"掌门人/创始人\":df_准备合并的列.values}\n",
    "df_准备合并列=pd.DataFrame(dict_准备合并的列)\n",
    "df_准备合并列.head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据框聚合(merge)\n",
    "\n",
    "## merge进行合并\n",
    "\n",
    "* 数据会被复制\n",
    "* 数目以多的一边为准"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### merge的参数\n",
    "on：列名，join用来对齐的那一列的名字，用到这个参数的时候一定要保证左表和右表用来对齐的那一列都有相同的列名。\n",
    "\n",
    "left_on：左表对齐的列，可以是列名，也可以是和dataframe同样长度的arrays。\n",
    "\n",
    "right_on：右表对齐的列，可以是列名，也可以是和dataframe同样长度的arrays。\n",
    "\n",
    "left_index/ right_index: 如果是True的haunted以index作为对齐的key\n",
    "\n",
    "how：数据融合的方法。\n",
    "\n",
    "sort：根据dataframe合并的keys按字典顺序排序，默认是，如果置false可以提高表现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>企业名称</th>\n",
       "      <th>掌门人/创始人</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>序号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "      <td>井贤栋</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>字节跳动</td>\n",
       "      <td>张一鸣</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>滴滴出行</td>\n",
       "      <td>程维</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Infor</td>\n",
       "      <td>Jim Schaper</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>JUUL Labs</td>\n",
       "      <td>Adam Bowen</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>492</td>\n",
       "      <td>Zipline International</td>\n",
       "      <td>Will Hetzler</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Ian Siegel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Joe Edmonds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Ward Poulos</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>493</td>\n",
       "      <td>ZipRecruiter</td>\n",
       "      <td>Willis Redd</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>911 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      企业名称        掌门人/创始人\n",
       "序号                                       \n",
       "0                     蚂蚁金服            井贤栋\n",
       "1                     字节跳动            张一鸣\n",
       "2                     滴滴出行             程维\n",
       "3                    Infor    Jim Schaper\n",
       "4                JUUL Labs     Adam Bowen\n",
       "..                     ...            ...\n",
       "492  Zipline International   Will Hetzler\n",
       "493           ZipRecruiter     Ian Siegel\n",
       "493           ZipRecruiter    Joe Edmonds\n",
       "493           ZipRecruiter    Ward Poulos\n",
       "493           ZipRecruiter    Willis Redd\n",
       "\n",
       "[911 rows x 2 columns]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df_独角兽_创始人 = pd.merge(df_待合并,df_准备合并的列,on= \"序号\")\n",
    "df_独角兽_创始人"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 企业名称对应多个投资公司\n",
    "* 首先把部分机构和所有企业名称取出来。把部分投资机构取出来split列表 字典企业名称 contains循环遍历每个投资方\n",
    "* 找某一投资方投资的所有企业 建字典contains做一个字典 企业名称作为values\n",
    "* 循环每个sublist取出来的item就是存到投资机构里面的值\n",
    "* .join把列表变成字符串用，隔开"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Merge两个表格进行合并，用，隔开 前后是两个表格，按照序号的索引\n",
    "* \\换下一行\n",
    "* stack按照序号\n",
    "* reset-index删掉第二级索引，用字典形式把返回的series（一维只有值和序号）变成dateframe(二维)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>投资方</th>\n",
       "      <th>独角兽企业</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>序号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>春华资本</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>春华资本</td>\n",
       "      <td>菜鸟网络</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>中投海外</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>红杉资本</td>\n",
       "      <td>蚂蚁金服</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>红杉资本</td>\n",
       "      <td>字节跳动</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>952</td>\n",
       "      <td>慕华投资</td>\n",
       "      <td>网易有道</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>953</td>\n",
       "      <td>GPI Capital</td>\n",
       "      <td>Zeta Global</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>954</td>\n",
       "      <td>GSO Capital Partners</td>\n",
       "      <td>Zeta Global</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>955</td>\n",
       "      <td>Visionnaire Ventures</td>\n",
       "      <td>Zipline International</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>956</td>\n",
       "      <td>Katalyst.Ventures</td>\n",
       "      <td>Zipline International</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2959 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                       投资方                  独角兽企业\n",
       "序号                                               \n",
       "0                     春华资本                   蚂蚁金服\n",
       "0                     春华资本                   菜鸟网络\n",
       "1                     中投海外                   蚂蚁金服\n",
       "2                     红杉资本                   蚂蚁金服\n",
       "2                     红杉资本                   字节跳动\n",
       "..                     ...                    ...\n",
       "952                   慕华投资                   网易有道\n",
       "953            GPI Capital            Zeta Global\n",
       "954   GSO Capital Partners            Zeta Global\n",
       "955   Visionnaire Ventures  Zipline International\n",
       "956      Katalyst.Ventures  Zipline International\n",
       "\n",
       "[2959 rows x 2 columns]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# C-1 直接给完整代码：\n",
    "df_投资方_企业名称 = df[[\"部分投资机构\",\"企业名称\"]]\n",
    "所有投资机构 = [item for sublist in df_投资方_企业名称[\"部分投资机构\"].str.split('[、,,]') for item in sublist]\n",
    "\n",
    "touzi_dict = {}\n",
    "for item in 所有投资机构:\n",
    "    df_投资方_item = df_投资方_企业名称[df_投资方_企业名称[\"部分投资机构\"].str.contains(item)]\n",
    "    touzi_dict[item] =','.join([i for i in df_投资方_item[\"企业名称\"].values])\n",
    "\n",
    "投资方_独角兽企业_Series = pd.Series(touzi_dict)\n",
    "\n",
    "df_投资方_独角兽企业 = pd.DataFrame({\"投资方\":投资方_独角兽企业_Series.index,\"独角兽企业\":投资方_独角兽企业_Series.values})\n",
    "df_投资方_独角兽企业.index.name=\"序号\"\n",
    "df_投资方_独角兽企业 =pd.merge(df_投资方_独角兽企业.drop('独角兽企业', axis=1),\\\n",
    "                     df_投资方_独角兽企业['独角兽企业'].str.split(',', expand=True).stack().reset_index(level=1,drop=True).rename('独角兽企业'),\\\n",
    "                     on=\"序号\")\n",
    "df_投资方_独角兽企业"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周小结\n",
    "1. 数据框新增变量\n",
    "  * 用str字符串方法處理文本數據\n",
    "  * assign新增一列\n",
    "2. 数据框重塑\n",
    "  * drop为删除某一列, split()拆分列元素, stack()是将原来的列索引转成了多层次索引\n",
    "3. 聚合\n",
    "  * merge数据会被复制,数目以多的一边为准"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "313.1875px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
